import { constantRouterMap } from '@/config/router.config';
import { Module } from 'vuex';
import { permissionState } from './types';
import { RootState } from '../../type';
import router from '@/router';
import { generatorDynamicRouter } from './routerUtil';
// 生成缓存路由
function getCacheRoutes(routes, cacheRoutes) {
    routes.forEach((item) => {
        if (item.children && item.children.length) {
            if (item.meta&&item.meta.keepAlive) {
                cacheRoutes.push(item.name);
            }
            getCacheRoutes(item.children, cacheRoutes);
        } else {
            if (item.meta&&item.meta.keepAlive) {
                cacheRoutes.push(item.name);
            }
        }
    });
    return cacheRoutes;
}

const state: permissionState = {
    routers: [],
    addRouters: [],
    cacheRoutes: []
};

type StateType = typeof state

const permission: Module<StateType, RootState> = {
    namespaced: true,
    state,
    mutations: {
        SET_ROUTERS: (state, routers) => {
            console.log('最终路由', router.getRoutes());
            state.addRouters = routers;
            state.routers = constantRouterMap.concat(routers);
            // console.log('最终路由表', state.routers);
        },
        SET_CACHEROUTES: (state, routes) => {
            // console.log('缓存路由名称', routes)
            state.cacheRoutes = routes;
        }
    },
    actions: {
        GenerateRoutes({ commit }) {
            return new Promise((resolve) => {
                // 动态生成路由
                generatorDynamicRouter().then((routes) => {
                    
                    // 找到根路由
                    const rootRoute = routes.find(route => route.path === '/');
                    // 如果找到根路由，将子应用添加到其 children 中
                    if (rootRoute && rootRoute.children) {
                        // 添加子应用路由
                        rootRoute.children.push(
                            {
                                path: 'app1/:page*',
                                name: 'app1',
                                component: () => import('@/views/subapp/index.vue'),
                                meta: {
                                    keepAlive: true,
                                    isHide: true
                                }
                            },
                            {
                                path: 'app2/:page*',
                                name: 'app2',
                                component: () => import('@/views/subapp/index.vue'),
                                meta: {
                                    keepAlive: true,
                                    isHide: true
                                }
                            },
                            {
                                path: 'app3/:page*',
                                name: 'app3',
                                component: () => import('@/views/subapp/index.vue'),
                                meta: {
                                    keepAlive: true,
                                    isHide: true
                                }
                            }
                        );
                    }

                    // 添加所有路由
                    routes.forEach((item) => {
                        router.addRoute(item);
                    });

                    

                    const cacheRoutes = getCacheRoutes(routes, []);
                    commit('SET_CACHEROUTES', cacheRoutes); // 生成缓存路由
                    commit('SET_ROUTERS', routes); // 生成最终路由
                    resolve('');
                });
            });
        }
    }
};

export default permission;
